(* output breathe pattern to red led with pwm module *)
MEM pwm_r = 1144;               (* address of pwm register *)
CONST duty_s = 6, en_s = 5;     (* shifts for bit fields *)
VAR base, duty, factor;

PROCEDURE delay;
    VAR cnt;
BEGIN
    cnt := 0;
    WHILE cnt < 100 DO
        cnt := cnt + 1
    END
END;

BEGIN
    factor := 0;
    duty := 0;
    base := 0;                  (* use 0 pin for pwm *)
    base := base + (1 << en_s); (* enable pwm *)
    
    WHILE 1 = 1 DO
        pwm_r := base + (duty << duty_s); (* write base and duty value *)
        
        duty := duty + factor;
        IF duty > 1023 THEN
            factor := -1
        END;
        IF duty = 0 THEN
            factor := 1
        END;
        
        CALL delay
    END
END.
